﻿using System;
using System.Linq.Expressions;
using System.Web.Http;
using System.Collections.Generic;
using Castle.Core.Logging;
using Hiver.Entity;
using Hiver.Business;
using Hiver.WebApi.Models;
using Hiver.Util.Helper;

namespace Hiver.WebApi.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    public class UserController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILogger Logger { get; set; }

        /// <summary>
        /// 数据服务
        /// </summary>
        public IUserService service { get; set; }

        /// <summary>
        /// 获得全部数据
        /// </summary>
        /// <returns></returns>
        [Route("api/User/List")]
        public BaseModel<List<UserInfo>> Get()
        {
            BaseModel<List<UserInfo>> response = new BaseModel<List<UserInfo>>();
            try
            {
                List<UserInfo> data = service.GetList();
                response.data = data;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = null;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获得数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BaseModel<UserInfo> Get(int id)
        {
            BaseModel<UserInfo> response = new BaseModel<UserInfo>();
            try
            {
                UserInfo data = service.Get(id);
                response.data = data;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = null;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获得分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("api/User/Page")]
        public BaseModel<List<UserInfo>> Get(int pageIndex = 1, int pageSize = 10)
        {
            BaseModel<List<UserInfo>> response = new BaseModel<List<UserInfo>>();
            try
            {
                List<UserInfo> data = service.GetPageList(pageIndex, pageSize);
                response.data = data;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = null;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获得查询数据
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("api/User/Search")]
        [HttpPost]
        public BasePageModel<List<UserInfo>> PostSearch(UserInfo query, int pageIndex = 1, int pageSize = 10)
        {
            BasePageModel<List<UserInfo>> response = new BasePageModel<List<UserInfo>>();
            try
            {
                Expression<Func<UserInfo, bool>> ex = (it => 1 == 1);
                if (!string.IsNullOrEmpty(query.UserNo))
                    ex = (it => it.UserNo.Contains(query.UserNo));
                if (!string.IsNullOrEmpty(query.UserName))
                    ex = (it => it.UserName.Contains(query.UserName));
                if (!string.IsNullOrEmpty(query.Access))
                    ex = (it => it.Access.Equals(query.Access));
                if (!string.IsNullOrEmpty(query.Email))
                    ex = (it => it.Email.Contains(query.Email));
                List<UserInfo> data = service.GetSearchPageList(ex, pageIndex, pageSize);
                response.total = service.GetList().Count;
                response.data = data;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = null;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public BaseModel<int> Post([FromBody] UserInfo info)
        {
            BaseModel<int> response = new BaseModel<int>();
            try
            {
                // 密码加密
                info.Password = SecurityHelper.MD5(info.Password);
                int result = service.Add(info);
                response.data = result;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = 0;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public BaseModel<bool> Put([FromBody] UserInfo info)
        {
            BaseModel<bool> response = new BaseModel<bool>();
            try
            {
                // 修改应该判断是否修改了密码
                info.Password = SecurityHelper.MD5(info.Password);
                bool result = service.Update(info);
                response.data = result;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = false;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BaseModel<bool> Delete(int id)
        {
            BaseModel<bool> response = new BaseModel<bool>();
            try
            {
                bool result = service.Del(id);
                response.data = result;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                response.data = false;
                response.code = 500;
                response.msg = ex.Message;
            }
            return response;
        }
    }
}